//
//  ExpListController.m
//  jang
//
//  Created by lee han on 18/06/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "ExpListController.h"
#import "CRUserData.h"
#import "ExpViewController.h"
#import "CommonView.h"

@interface ExpListController (){
    NSString* m_expContent;
}

@end

@implementation ExpListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    _mTableView.delegate = self;
    _mTableView.dataSource = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary* info = [[[CRUserData sharedInstance] getExpData] objectAtIndex:indexPath.row];
    
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:[info objectForKey:@"id"], @"eid", nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_ExpDetail];
}
//end UITableViewDelegate

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[[CRUserData sharedInstance] getExpData] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"subtitlecell"];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"subtitlecell"];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
    }
    
    NSDictionary* data = [[[CRUserData sharedInstance] getExpData] objectAtIndex:indexPath.row];
    [cell.textLabel setText:[data objectForKey:@"title"]];
    [cell.detailTextLabel setText:[data objectForKey:@"title"]];
    
    return cell;
}

//end UITableViewDataSource


#pragma mark networkDelegate

- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if(type == NetworkFunctionType_ExpDetail) {
        if([[response objectForKey:@"status"] integerValue] == 1){
            m_expContent = [response objectForKey:@"content"];
            
            [self performSegueWithIdentifier:@"expltoexp" sender:self];
        }
        else{
            [CommonView addAlertView:self Text:@"生意经内容错误"];
        }
    }
}
//end networkDelegate

- (void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id)sender{
    ExpViewController *destination = segue.destinationViewController;
    destination.mContentText = m_expContent;
}

@end
